


#ifndef PF_TEXT_H
#define PF_TEXT_H

#include "pf_entitycontainer.h"
#include "lx_Export.h"

/**
 * Holds the data that defines a text entity.
 */
class LX_EXPORT PF_TextAttribute {
public:
    /**
     * Default constructor. Leaves the data object uninitialized.
     */
    PF_TextAttribute() {}

    /**
     * Constructor with initialisation.
     *
     * @param insertionPoint Insertion point
     * @param height Nominal (initial) text height
     * @param width Reference rectangle width
     * @param valign Vertical alignment
     * @param halign Horizontal alignment
     * @param drawingDirection Drawing direction
     * @param lineSpacingStyle Line spacing style
     * @param lineSpacingFactor Line spacing factor
     * @param text Text string
     * @param style Text style name
     * @param angle Rotation angle
     * @param updateMode LX_Define::Update will update the text entity instantly
     *    LX_Define::NoUpdate will not update the entity. You can update
     *    it later manually using the update() method. This is
     *    often the case since you might want to adjust attributes
     *    after creating a text entity.
     */
    PF_TextAttribute(const PF_Vector& insertionPoint,
                double height,
                double width,
                LX_Define::VAlign valign,
                LX_Define::HAlign halign,
                LX_Define::TextDrawingDirection drawingDirection,
                LX_Define::TextLineSpacingStyle lineSpacingStyle,
                double lineSpacingFactor,
                const QString& text,
                const QString& style,
                double angle,
                LX_Define::UpdateMode updateMode = LX_Define::Update) {
        this->insertionPoint = insertionPoint;
        this->height = height;
        this->width = width;
        this->valign = valign;
        this->halign = halign;
        this->drawingDirection = drawingDirection;
        this->lineSpacingStyle = lineSpacingStyle;
        this->lineSpacingFactor = lineSpacingFactor;
        this->style = style;
        this->angle = angle;
        this->text = text;
        this->updateMode = updateMode;
    }

    friend class PF_Text;

    friend std::ostream& operator << (std::ostream& os, const PF_TextAttribute& td) {
        os << "(" << td.text.toLatin1().data() << ")";
        return os;
    }

public:
    /** Insertion point */
    PF_Vector insertionPoint;
    /** Nominal (initial) text height */
    double height;
    /** Reference rectangle width */
    double width;
    /** Vertical alignment */
    LX_Define::VAlign valign;
    /** Horizontal alignment */
    LX_Define::HAlign halign;
    /** Drawing direction */
    LX_Define::TextDrawingDirection drawingDirection;
    /** Line spacing style */
    LX_Define::TextLineSpacingStyle lineSpacingStyle;
    /** Line spacing factor */
    double lineSpacingFactor;
    /** Text string */
    QString text;
    /** Text style name */
    QString style;
    /** Rotation angle */
    double angle;
    /** Update mode */
    LX_Define::UpdateMode updateMode;
};



/**
 * Class for a text entity.
 * Please note that text strings can contain special 
 * characters such as %%c for a diameter sign as well as unicode
 * characters. Line feeds are stored as real line feeds in the string.
 *

 */
class LX_EXPORT PF_Text : public PF_Container {
public:
    PF_Text(PF_Container* parent,
            const PF_TextAttribute& d);
    virtual ~PF_Text() {}

    virtual PF_Entity* clone() {
        PF_Text* t = new PF_Text(*this);
        t->setOwner(isOwner());
        t->initId();
        t->detach();
        return t;
    }

    /**	@return LX_Define::EntityText */
    virtual LX_Define::EntityType rtti() const {
        return LX_Define::EntityText;
    }

    /** @return Copy of data that defines the text. */
    PF_TextAttribute getData() const {
        return data;
    }

    void update();
    void updateAddLine(PF_Container* textLine, int lineCounter);

    int getNumberOfLines();


    PF_Vector getInsertionPoint() {
        return data.insertionPoint;
    }
    double getHeight() {
        return data.height;
    }
    void setHeight(double h) {
        data.height = h;
    }
    double getWidth() {
        return data.width;
    }
    void setAlignment(int a);
    int getAlignment();
    LX_Define::VAlign getVAlign() {
        return data.valign;
    }
	void setVAlign(LX_Define::VAlign va) {
		data.valign = va;
	}
    LX_Define::HAlign getHAlign() {
        return data.halign;
    }
	void setHAlign(LX_Define::HAlign ha) {
		data.halign = ha;
	}
    LX_Define::TextDrawingDirection getDrawingDirection() {
        return data.drawingDirection;
    }
    LX_Define::TextLineSpacingStyle getLineSpacingStyle() {
        return data.lineSpacingStyle;
    }
    void setLineSpacingFactor(double f) {
        data.lineSpacingFactor = f;
    }
    double getLineSpacingFactor() {
        return data.lineSpacingFactor;
    }
    void setText(const QString& t);
    QString getText() {
        return data.text;
    }
    void setStyle(const QString& s) {
        data.style = s;
    }
    QString getStyle() {
        return data.style;
    }
	void setAngle(double a) {
		data.angle = a;
	}
    double getAngle() {
        return data.angle;
    }
    double getUsedTextWidth() {
        return usedTextWidth;
    }
    double getUsedTextHeight() {
        return usedTextHeight;
    }

	virtual double getLength() {
		return -1.0;
	}
	
    virtual PF_VectorSolutions getRefPoints();
    virtual PF_Vector getNearestRef(const PF_Vector& coord,
                                     double* dist = NULL);

    virtual void move(PF_Vector offset);
    virtual void rotate(PF_Vector center, double angle);
    virtual void scale(PF_Vector center, PF_Vector factor);
    virtual void mirror(PF_Vector axisPoint1, PF_Vector axisPoint2);
	virtual bool hasEndpointsWithinWindow(PF_Vector v1, PF_Vector v2);
    virtual void stretch(PF_Vector firstCorner,
                         PF_Vector secondCorner,
                         PF_Vector offset);

    friend std::ostream& operator << (std::ostream& os, const PF_Text& p);

protected:
    PF_TextAttribute data;

    /**
     * Text width used by the current contents of this text entity. 
     * This property is updated by the update method.
     * @see update
     */
    double usedTextWidth;
    /**
     * Text height used by the current contents of this text entity.
     * This property is updated by the update method.
     * @see update
     */
    double usedTextHeight;
};

#endif
